package animator.phantom.renderer.plugin;

/*
    Copyright Janne Liljeblad 2006,2007,2008

    This file is part of Phantom2D.

    Phantom2D is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Phantom2D is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Phantom2D.  If not, see <http://www.gnu.org/licenses/>.
*/

import animator.phantom.plugin.*;
import animator.phantom.renderer.param.*;
import animator.phantom.paramedit.*;
import giotto2D.filters.color.Threshold;
import giotto2D.filters.color.Invert;
import com.jhlabs.image.GaussianFilter;
import giotto2D.filters.color.SmoothThreshold;
import java.awt.image.*;
import java.awt.geom.*;
import java.awt.*;
import java.util.*;

public class AlienNeonPlugin extends PhantomPlugin
{
	private int initialThresh = 128;
	private int stepThresh = 80;
	private int stepBlur = 15;
	private float featherBlur = 2.5f;


	public AlienNeonPlugin()
	{
		initPlugin( FILTER );
	}

	public void buildDataModel()
	{
		setName( "AlienNeon" );

	}

	public void buildEditPanel()
	{

	}

	public void doImageRendering( int frame )
	{
		BufferedImage flowImg = getFlowImage();
		blur( flowImg, stepBlur );
		thresholdAndFeather( flowImg, initialThresh );

		BufferedImage[] layers = new BufferedImage[7];
		layers[0] = flowImg;
		for( int i = 0; i < 6; i++ )
		{
			BufferedImage nextLayer = PluginUtils.getImageClone( layers[ i ] );
			blur( nextLayer, stepBlur );
			thresholdAndFeather( nextLayer, stepThresh );
			layers[ i + 1 ] = nextLayer;
		}

		BufferedImage out = PluginUtils.createCanvas( flowImg.getWidth(), flowImg.getHeight() );
		for( int i = 6; i > -1; i-- )
		{
			if( i%2 == 0)
			{
				System.out.println( "l");
				PluginUtils.doAlignedBlend( out, layers[ i ], 1.0f, PluginUtils.LIGHTEN );
			}
			else
			{

				System.out.println( "d");
				Invert.filter( layers[ i ] );
				PluginUtils.doAlignedBlend( out, layers[ i ], 1.0f, PluginUtils.DARKEN );
			}
		}

		sendFilteredImage( out, frame );
	}

	private void thresholdAndFeather( BufferedImage img, int threshold )
	{
		Threshold.filter( img, threshold, Color.black, Color.white );
		blur(img, featherBlur);
	}

	private void blur( BufferedImage img, float radius )
	{
		GaussianFilter f =  new GaussianFilter();
		f.setRadius( radius);
		f.setUseAlpha( true );
		Graphics2D gc = img.createGraphics();
		gc.drawImage( img, f, 0, 0);
		gc.dispose();
	}

}//end class
